package manager_web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import manager_service.exception.CourseNotExistException;
import manager_service.service.BackCourseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import test_common.constant.Constant;
import test_common.entity.Course;
import test_common.utils.ContentIsNull;
import test_common.utils.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhangzheng
 * @create 2020/12/25
 */

@Controller
@RequestMapping("/course")
public class CourseController {

    @Reference
    private BackCourseService backCourseService;

    //添加课程
    @ResponseBody
    @RequestMapping("/addCourse")
    public ResponseResult addCourse(HttpServletRequest request, @RequestParam(value = "cover_image",required = false) MultipartFile cover_image){
        ResponseResult rs=new ResponseResult();
        String courseName=request.getParameter("course_name");
        String courseInfo=request.getParameter("course_info");
        String author=request.getParameter("author");
        String recommendationGrade=request.getParameter("recommendation_grade");
        String courseTypeId=request.getParameter("course_type_id");
//        String cp = request.getServletContext().getRealPath("/upload/");
        if (ContentIsNull.isNull(courseName)||ContentIsNull.isNull(courseInfo)||ContentIsNull.isNull(author)||
                ContentIsNull.isNull(recommendationGrade) ||ContentIsNull.isNull(courseTypeId)){
            rs.setMessage("必填内容不可为空，请重新填写");
            rs.setStatus(Constant.RESPONSE_FAILED);
            return rs;
        }
        String cp = "/upload/coverImg/";
        String realCp="F:/netclass/test_manager/manager_web/src/main/resources/static/upload/coverImg/";
        String coverImageUrl=null;
        String simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        if(!cover_image.isEmpty()){
            try {
                coverImageUrl=cp+simpleDateFormat+cover_image.getOriginalFilename();
//                coverImageUrl=cover_imagele.getOriginalFilename();
                cover_image.transferTo(new File(realCp+simpleDateFormat+cover_image.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Date createDate=new Date();
        Integer clickNumber=0;
        Integer status= Constant.COURSE_ENABLE;
        Course course=new Course(courseName,courseInfo,author,coverImageUrl,createDate,clickNumber,status,Integer.parseInt(recommendationGrade),Integer.parseInt(courseTypeId));
        backCourseService.addCourse(course);
        rs.setMessage("添加成功");
        rs.setStatus(Constant.RESPONSE_SUCCESS);
        return rs;
    }

    //修改课程
    @ResponseBody
    @RequestMapping("/modifyCourse")
    public ResponseResult modifyCourse(HttpServletRequest request, @RequestParam(value = "cover_image",required = false) MultipartFile cover_image){
        ResponseResult rs=new ResponseResult();
        String id=request.getParameter("course_id");
        String courseName=request.getParameter("course_name");
        String courseInfo=request.getParameter("course_info");
        String author=request.getParameter("author");
        String recommendationGrade=request.getParameter("recommendation_grade");
        String courseTypeId=request.getParameter("course_type_id");
        if (ContentIsNull.isNull(courseName)||ContentIsNull.isNull(courseInfo)||ContentIsNull.isNull(author)||
                ContentIsNull.isNull(recommendationGrade) ||ContentIsNull.isNull(courseTypeId)){
            rs.setMessage("必填内容不可为空，请重新填写");
            rs.setStatus(Constant.RESPONSE_FAILED);
            return rs;
        }
        String cp = "/upload/coverImg/";
        String realCp="F:/netclass/test_manager/manager_web/src/main/resources/static/upload/coverImg/";
        String coverImageUrl=null;
        String simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Course course=null;
        Course course1= null;
        try {
            course1 = backCourseService.selectCourse(Integer.parseInt(id));
        } catch (CourseNotExistException e) {
            e.printStackTrace();
        }
        if(!cover_image.isEmpty()){
                coverImageUrl=cp+simpleDateFormat+cover_image.getOriginalFilename();
//                coverImageUrl=cover_imagele.getOriginalFilename();
            try {
                cover_image.transferTo(new File(realCp+simpleDateFormat+cover_image.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            coverImageUrl=course1.getCoverImageUrl();
        }
        Date createDate=new Date();
        Integer clickNumber=course1.getClickNumber();
        Integer status= course1.getStatus();
         course=new Course(Integer.parseInt(id),courseName,courseInfo,author,coverImageUrl,createDate,clickNumber,status,Integer.parseInt(recommendationGrade),Integer.parseInt(courseTypeId));

         backCourseService.modifyCourse(course);
         rs.setStatus(Constant.RESPONSE_SUCCESS);
         rs.setMessage("修改成功");
         return rs;
    }

    //所有课程
    @RequestMapping("/showCourseList")
    public ModelAndView showCourseList(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.Course_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.Course_DEFAULT_PAGE_NO;
        }
        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        PageInfo<Course> pageCourses=backCourseService.showCourseList(pageNoint,pageSizeint);
        mav.addObject("pageCourses",pageCourses);
        mav.setViewName("back_courseSet");
        return mav;
    }


    @ResponseBody
    @RequestMapping("/selectCourseById")
    public  Course selectCourseById(HttpServletRequest request){
        String id=request.getParameter("id");
        Course course= null;
        try {
            course = backCourseService.selectCourse(Integer.parseInt(id));
        } catch (CourseNotExistException e) {
            e.printStackTrace();
        }
        return course;
    }

    //启用禁用课程
    @ResponseBody
    @RequestMapping("/modifyCourseStatus")
    public ResponseResult modifyCourseStatus(HttpServletRequest request){
        ResponseResult rs=new ResponseResult();
        String id=request.getParameter("id");
        String status=request.getParameter("status");
        Course course=new Course();
        if (ContentIsNull.isNull(id)){
            rs.setStatus(Constant.RESPONSE_FAILED);
            rs.setMessage("数据异常，没有改课程");
        }
        course.setId(Integer.parseInt(id));
        if (Integer.parseInt(status)==1){
            course.setStatus(Constant.COURSE_DISABLE);
        }
        else if (Integer.parseInt(status)==0){
           course.setStatus(Constant.COURSE_ENABLE);
        }
        backCourseService.modifyCourseStatus(course);
        rs.setMessage("状态修改成功");
        rs.setStatus(Constant.RESPONSE_SUCCESS);
        return  rs;

    }

    @RequestMapping("/searchCourses")
    public ModelAndView searchCourses(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String author=request.getParameter("author");
        String courseName=request.getParameter("courseName");
        String status=request.getParameter("status");
        String courseTypeId=request.getParameter("courseTypeId");
        String createDateStart=request.getParameter("createDateStart");
        String createDateEnd=request.getParameter("createDateEnd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ContentIsNull.isNull(createDateStart)){
            createDateStart="2020-05-01 00:00:00";
        }
        if (ContentIsNull.isNull(createDateEnd)){
            createDateEnd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.Course_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.Course_DEFAULT_PAGE_NO;
        }
        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        try {
            PageInfo<Course> pageSearchCourses=backCourseService.searchCourses(author,courseName,status,courseTypeId,
                    simpleDateFormat.parse(createDateStart),
                    simpleDateFormat.parse(createDateEnd),
                    pageNoint,pageSizeint);

            mav.setViewName("back_courseSearch_result");
            mav.addObject("pageSearchCourses",pageSearchCourses);
            mav.addObject("author",author);
            mav.addObject("courseName",courseName);
            mav.addObject("status",status);
            mav.addObject("courseTypeId",courseTypeId);
            mav.addObject("createDateStart",createDateStart);
            mav.addObject("createDateEnd",createDateEnd);
            return mav;
        } catch (ParseException e) {
            e.printStackTrace();
        }


        mav.setViewName("back_courseSet");
        mav.addObject("message","数据异常，搜索不到");
        return  mav;
    }
}
